import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:netease_music/player/controller/bottom_player_overlay_controller.dart';

class RouteObserverWithPlayer extends NavigatorObserver {
  final overlayController = Get.find<BottomPlayerOverlayController>();

  void _updateBottom(Route<dynamic> route) {
    final routeName = route.settings.name ?? '';
    if (routeName == '/fullPlayer') {
      Future.delayed(const Duration(milliseconds: 100), () {
        overlayController.setBottom(-50);
      });
    } else {
      Future.delayed(const Duration(milliseconds: 100), () {
        overlayController.setBottom(0);
      });
    }
  }

  @override
  void didPush(Route route, Route? previousRoute) {
    _updateBottom(route);
    super.didPush(route, previousRoute);
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    if (previousRoute != null) {
      _updateBottom(previousRoute);
    }
    super.didPop(route, previousRoute);
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    if (newRoute != null) {
      _updateBottom(newRoute);
    }
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
  }
}
